package com.ymatou.iapi.shoppingcartservice.testcase;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductGetInfoBySellerIdReq;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductGetInfoBySellerIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_ShoppingcartProductGetInfoBySellerId {
	
	private static ShoppingcartProductGetInfoBySellerIdCall shoppingcartProductGetInfoBySellerIdCall = new ShoppingcartProductGetInfoBySellerIdCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();
	private static Integer sellerId = 33832;

	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据sellerid获取购物车商品信息");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductGetInfoBySellerIdCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoBySellerId_001() throws Exception {
		Logger.start(true,"根据sellerid获取单个购物车商品信息（默认source为0）");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(userId);
			req.setSellerId(sellerId);
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject data = ret.getJSONObject("data");
			JSONArray list = data.getJSONArray("list");
			JSONObject o = list.getJSONObject(0);
			ShoppingcartProductBean beana = ProductManagerCallService.getBeanFromJson(o, ShoppingcartProductBean.class);

			Logger.verifyEquals(200, code, "接口code返回异常");
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(beana);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByProductId_002() throws Exception {
		Logger.start(true,"输入错误的sellerid获取购物车商品信息");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(userId);
			req.setSellerId(42131);;
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject data = ret.getJSONObject("data");
			JSONArray list = data.getJSONArray("list");
			int length = list.length();


			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(0, length, "data数据返回异常");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByProductId_003() throws Exception {
		Logger.start(true,"输入错误的userid获取购物车商品信息");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(1);
			req.setSellerId(sellerId);
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject data = ret.getJSONObject("data");
			JSONArray list = data.getJSONArray("list");
			int length = list.length();

			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(0, length, "data数据返回异常");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByProductId_004() throws Exception {
		Logger.start(true,"根据sellerid获取批量购物车商品信息（默认source为0）");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId, "catalogId1");
			buyerShoppingCartService.getShoppingCartProduct(userId, "catalogId2");
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(userId);
			req.setSellerId(sellerId);
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject data = ret.getJSONObject("data");
			JSONArray list = data.getJSONArray("list");
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			
			for (int i = 0; i < list.length(); i++) {
				JSONObject o = list.getJSONObject(i);
				ShoppingcartProductBean beana = ProductManagerCallService.getBeanFromJson(o, ShoppingcartProductBean.class);
				
				buyerShoppingCartService.checkShoppingcartProductBeanFromDB(beana);
			}
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByProductId_005() throws Exception {
		Logger.start(true,"根据sellerid和source获取购物车商品信息");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(userId);
			req.setSellerId(sellerId);
			req.setSource(1);
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject data = ret.getJSONObject("data");
			JSONArray list = data.getJSONArray("list");
			JSONObject o = list.getJSONObject(0);
			ShoppingcartProductBean beana = ProductManagerCallService.getBeanFromJson(o, ShoppingcartProductBean.class);

			Logger.verifyEquals(200, code, "接口code返回异常");
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(beana);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByProductId_006() throws Exception {
		Logger.start(true,"输入错误的source获取购物车商品信息");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(userId);
			req.setSource(0);
			req.setSellerId(sellerId);
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String data = ret.getString("data");

			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals("null", data, "data数据返回异常");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_006() throws Exception {
		Logger.start(true,"不传入userid");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(0);
			req.setSource(0);
			req.setSellerId(sellerId);
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("userId:错误的userId;", message, "不传入userid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoByCatalogId_007() throws Exception {
		Logger.start(true,"不传入sellerid");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoBySellerIdReq req = new ShoppingcartProductGetInfoBySellerIdReq();
			
			req.setUserId(userId);
			req.setSource(0);
			req.setSellerId(sellerId);
			
			JSONObject ret = shoppingcartProductGetInfoBySellerIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("sellerId:错误的sellerId;", message, "不传入sellerid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
